package proxy;

import org.apache.http.HttpEntity;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.conn.HttpHostConnectException;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.proxy.Proxy;
import us.codecraft.webmagic.proxy.ProxyProvider;
import util.Config;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class IpPoolProxyProvider implements ProxyProvider {
    protected Logger logger = LoggerFactory.getLogger(getClass());

    public IpPoolProxyProvider(){

    }
    @Override
    public void returnProxy(Proxy proxy, Page page, Task task) {
        //Donothing
    }
    @Override
    public Proxy getProxy(Task task) {
        return getRandomProxy();
    }
    private Proxy getRandomProxy(){

        CloseableHttpClient httpClient = HttpClients.createDefault();
        //ip池的代理 如果ip代理池无连接失败使用本机的ip进行抓取。
        HttpGet get = new HttpGet(Config.getCrawler_IP_POOL());
        CloseableHttpResponse response = null;
        String Host="";
        int port=0;
        try {
            response = httpClient.execute(get);
            if(response != null && response.getStatusLine().getStatusCode() == 200)
            {
                HttpEntity entity  = response.getEntity();
                String content= EntityUtils.toString(entity);
                String[] ls = content.split(":");
                Host = ls[0];
                port = Integer.parseInt(ls[1]);

            }
        }catch (HttpHostConnectException he){
            logger.warn("can not connect to IP pool!");
            return null;
        }
        catch (ArrayIndexOutOfBoundsException e) {
            logger.warn("Invalid Ip format! Ip pool may not usable right now!");
            return null;
        }
        catch (Exception e){
            e.printStackTrace();
        }
        return new Proxy(Host,port);
    }
}
